<template>
  <el-container style="height:550px; border: 1px solid #eee">
      <el-main style="overflow: hidden;">
          <table style="width:100%; font-size:12px;">
              <tr v-if="germmode" v-show="!bgermdisabled">
                  <td style="height:26px; padding-left:60px; " colspan="4">
                      <el-radio-group @change="germflagchange" v-model="germflag">
                          <el-radio :disabled="bgermdisabled" :label="4">涂片结果</el-radio>
                          <el-radio :disabled="bgermdisabled" :label="1">阴性结果</el-radio>
                          <el-radio :disabled="bgermdisabled" :label="2">致病菌</el-radio>
                          <el-radio :disabled="bgermdisabled" :label="3">正常菌种</el-radio>
                          <el-radio :disabled="bgermdisabled" :label="5">细菌其他</el-radio>
                          <el-radio :disabled="bgermdisabled" :label="99">全部</el-radio>
                      </el-radio-group>
                  </td>
              </tr>
              <tr v-else>
                  <td style="max-width:60px;min-width:60px;">报告单元</td>
                  <td style="width:50%;">
                      <rm-ddl-rptunit :prgno="prgno" :appendtobody="true" :disabled="rptunitdisabled" @change="handlerptunitchange" v-model="myrptunitid" width="100%"></rm-ddl-rptunit>
                  </td>
                  <td style="max-width:60px;min-width:60px;">项目分类</td>
                  <td style="width:50%;">
                      <rm-ddl-rptitemclass @change="handleitemclasschange" v-model="myitemclass" width="100%"></rm-ddl-rptitemclass>
                  </td>
              </tr>
              <tr>
                  <td colspan="4">
                      <el-input @keyup.native="searchkeydown" @input="autosearch" placeholder="快速检索" prefix-icon="el-icon-search" ref="qcodeinput" style="width:300px" v-model="qcode" :clearable="true"></el-input>
                      <el-button @click="btncancel" icon="el-icon-circle-close" style="float:right; margin-left:10px;" type="primary">取消</el-button>
                      <el-button @click="btnok" icon="el-icon-circle-check" style="float:right; margin -right:10px;" type="primary">确定</el-button>
                      <el-button @click="refreshdata" icon="el-icon-refresh" style="float:right" type="primary">刷新数据</el-button>
                  </td>
              </tr>
          </table>

          <vxe-table :data.sync="tableData" :select-config="selectConfig" height="auto" :sort-config="{ trigger: 'cell' }" :loading="loading" highlight-current-row highlight-hover-row border fit highlight-hover-column resizable header-align="center" show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe ref="ddlrptitemTable">
              <vxe-table-column type="selection" title="" width="40" align="center"></vxe-table-column>
              <vxe-table-column :field="items.field" :key="items.field" :title="items.title" :width="items.width" sortable v-for="items in tableColumn"></vxe-table-column>
          </vxe-table>
      </el-main>
  </el-container>
</template>
<script>
export default {
  props: ["rptunitid", "germmode", "isAllRpt", "itemclass", "rptunitdisabled", "prgno", "initgermflag", "germdisabled", "currptitem"],
  data() {
      return {
          fulldata: null,
          myrptunitid: this.rptunitid,
          myitemclass: this.itemclass,
          germflag: this.initgermflag ? this.initgermflag : 99,
          bgermdisabled: !!this.germdisabled,
          selrow: null,
          qcode: "",
          clsMenus: [],
          tableColumn: [],
          selectConfig: { tigger: "row" },
          tableData: [],
          seloptions: [],
          loading: false,
      };
  },
  computed: {},
  updated() {
      this.$nextTick(() => {
          // this.rptunitid = this.rptunitid;
          this.myrptunitid = this.rptunitid; //加上后报告单元不可选择
          //如果输入框内有值，直接定位到该报告项目
          if (this.tableData.length > 0) {
              if (this.currptitem) {
                  let currow = this.tableData.find((item) => item.rpt_itemcode === this.currptitem);
                  this.$refs.ddlrptitemTable.setCurrentRow(currow);
                  this.$refs.ddlrptitemTable.scrollToRow(currow);
              } else {
                  this.$refs.ddlrptitemTable.setCurrentRow(this.tableData[0]);
              }
          }
      });
  },
  methods: {
      autosearch: function (evt) {
          if (evt.keyCode != 13 && evt.keyCode != 40 && evt.keyCode != 38) {
              this.filterdata();
          }
      },
      germflagchange: function () {
          this.filterdata();
      },
      changeactiverow: function (cindex) {
          var row = this.$refs.ddlrptitemTable.getCurrentRow();
          var ridx = this.$refs.ddlrptitemTable.getRowIndex(row);
          var newidx = ridx + cindex;
          if (newidx < 0) newidx = 0;
          if (newidx >= this.tableData.length) {
              newidx = 0;
          }
          var newrow = this.tableData[newidx];
          this.$refs.ddlrptitemTable.setCurrentRow(newrow);
          this.$nextTick(() => {
              if (this.$refs.ddlrptitemTable) {
                  var crow = this.$refs.ddlrptitemTable.getCurrentRow();
                  this.$refs.ddlrptitemTable.scrollToRow(crow);
              }
          });
      },
      searchkeydown: function (evt) {
          if (evt.keyCode == 13) {
              let row = this.$refs.ddlrptitemTable.getCurrentRow();
              this.$emit("selitem", row.rpt_itemcode, row.rpt_itemname, [row]);
          } else if (evt.keyCode == 40) {
              var cindex = 1;
              this.changeactiverow(cindex);
          } else if (evt.keyCode == 38) {
              var cindex = -1;
              this.changeactiverow(cindex);
          }
      },
      handlerptunitchange: function (val) {
          this.myrptunitid = val;
          this.filterdata();
      },
      handleitemclasschange: function (val, sobj) {
          this.myitemclass = val;
          this.filterdata();
      },
      async filterdata() {
          try {
              if (this.germmode) {
                  var cData = await this.$ddldata.getallrptitem();
                  if (this.germflag == 99) {
                      cData = cData.filter((item) => item.germflagid != "0");
                  } else {
                      cData = cData.filter((item) => item.germflagid == this.germflag);
                  }
                  if (this.qcode != "") {
                      cData = cData.filter((item) => (item.rpt_itemcode ? item.rpt_itemcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.rpt_itemname ? item.rpt_itemname.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.inputcode ? item.inputcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.itemcode_en ? item.itemcode_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.itemname_en ? item.itemname_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false));
                  }
                  if (cData.length > 0) {
                      this.selrow = cData[0];
                  } else {
                      this.selrow == null;
                  }
                  this.tableData = cData;
                  this.$nextTick(() => {
                      if (this.$refs.ddlrptitemTable) {
                          if (this.selrow) {
                              this.$refs.ddlrptitemTable.setCurrentRow(this.selrow);
                          }
                      }
                  });
              } else {
                  if (this.myrptunitid && this.myrptunitid != 0) {
                      var cData = await this.$ddldata.getrptunititem(this.myrptunitid);
                  } else {
                      cData = await this.$ddldata.getitem(this.isAllRpt);
                  }
                  if (this.myitemclass && this.myitemclass != "") {
                      cData = cData.filter((item) => item.itemclasscode == this.myitemclass || item.parentclasscode == this.myitemclass);
                  }
                  if (this.qcode != "") {
                      cData = cData.filter((item) => (item.rpt_itemcode ? item.rpt_itemcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.rpt_itemname ? item.rpt_itemname.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.inputcode ? item.inputcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.itemcode_en ? item.itemcode_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false) || (item.itemname_en ? item.itemname_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 : false));
                  }

                  if (cData.length > 0) {
                      this.selrow = cData[0];
                  } else {
                      this.selrow == null;
                  }
                  this.tableData = cData;
                  this.$nextTick(() => {
                      if (this.$refs.ddlrptitemTable) {
                          if (this.selrow) {
                              this.$refs.ddlrptitemTable.setCurrentRow(this.selrow);
                          }
                      }
                  });
              }
          } catch (e) {
              console.log("erro", e);
          }
      },

      refreshdata: function () {
          this.$ddldata.rmallrptitem = "";
          this.$ddldata.rmrptitem = "";
          this.$ddldata.rmrptunititem = [];
          this.loadAllData();
      },
      async loadAllData() {
          this.loading = true;
          var res = await this.$ddldata.getrmrptitem();
          this.filterdata();
          this.loading = false;
      },
      btncancel() {
          this.$emit("cancel");
      },
      btnok: function () {
          this.myitemclass = "";
          //this.myrptunitid = "";
          this.qcode = "";

          if (this.qcode == "" && this.myitemclass == "") {
              this.filterdata();
              setTimeout(() => {
                  let data = this.$refs.ddlrptitemTable.getSelectRecords();
                  this.$emit("selitem", "", "", data);
              }, 20);
          }
      },

      setcol() {
          if (this.germmode) {
              this.tableColumn = [
                  { field: "itemcode_en", title: "英文缩写", width: 100, sortable: true },
                  {
                      field: "rpt_itemname",
                      title: "细菌项目名称",
                      width: 200,
                      sortable: true,
                  },
                  { field: "itemname_en", title: "英文名称", width: 200, sortable: true },
                  { field: "seq", title: "使用频次", width: 60, sortable: true },
                  {
                      field: "isgermgroup",
                      title: "是否菌属",
                      width: 100,
                      sortable: true,
                      formatter: function ({ cellValue, row, rowIndex, column, columnIndex }) {
                          if (cellValue == "1") return "是";
                          else return "";
                      },
                  },
                  {
                      field: "groupitemcode",
                      title: "所属菌属",
                      width: 100,
                      sortable: true,
                  },
                  {
                      field: "rpt_itemcode",
                      title: "项目代码",
                      width: 100,
                      sortable: true,
                  },
                  { field: "inputcode", title: "助记符", width: 100, sortable: true },
              ];
          } else {
              this.tableColumn = [
                  {
                      field: "itemclassname",
                      title: "项目归类",
                      width: 100,
                      sortable: true,
                  },
                  {
                      field: "rpt_itemcode",
                      title: "项目代码",
                      width: 100,
                      sortable: true,
                  },
                  { field: "itemcode_en", title: "英文缩写", width: 100, sortable: true },
                  {
                      field: "rpt_itemname",
                      title: "报告项目名称",
                      width: 200,
                      sortable: true,
                  },
                  { field: "unit", title: "单位", width: 100, sortable: true },
                  { field: "ref_text", title: "参考值", width: 200, sortable: true },
                  { field: "itemtips", title: "项目备注", width: 200, sortable: true },
                  {
                      field: "instrclassname",
                      title: "仪器类别",
                      width: 100,
                      sortable: true,
                  },
                  { field: "inputcode", title: "助记符", width: 100, sortable: true },
              ];
          }
      },
  },
  mounted() {
      this.loadAllData();
      this.setcol();
  },
  watch: {
      rptunitid(val) {
          if (val == "") {
              this.myrptunitid = 0;
          } else {
              if (isNaN(val)) {
                  this.myrptunitid = 0;
              } else {
                  var intval = parseInt(val);
                  this.myrptunitid = intval;
              }
          }
          this.filterdata();
      },
  },
};
</script>
<style scoped>
.el-main {
  padding: 0;
}
</style>
